/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.Berita;
import entity.DaftarBerita;
import entity.User;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.exceptions.NonexistentEntityException;

/**
 *
 * @author andhika
 */
public class ServletBeritaHelper {

    HttpServletRequest request;

    public ServletBeritaHelper(HttpServletRequest request) {
        this.request = request;
    }

    public String create() {
        HttpSession session = request.getSession();
        List<Berita> daftarBerita = new ArrayList<Berita>();
        Berita berita = new Berita();
        DaftarBerita db = new DaftarBerita();

        String judulBerita = request.getParameter("judulBerita");
        String ringkasan = request.getParameter("ringkasan");
        String kategori = request.getParameter("kategori");
        String deskripsi = request.getParameter("deskripsi");

        berita.setJudulBerita(judulBerita);
        berita.setRingkasan(ringkasan);
        berita.setDeskripsi(deskripsi);
        berita.setKategori(kategori);
        if (judulBerita.equals("") || ringkasan.equals("") || deskripsi.equals("")) {
            session.setAttribute("ulangi", "semua field harus terisi");
            return "beritaTambah.jsp";
        }
        db.addBerita(berita);

        daftarBerita = db.getBerita();
        request.setAttribute("daftarBerita", daftarBerita);
        return "beritaDaftar.jsp";
    }

    public String hapusBerita() throws NonexistentEntityException {
        DaftarBerita db = new DaftarBerita();
        HttpSession session = request.getSession();
        List<Berita> daftarBerita = new ArrayList<Berita>();
        if (session.getAttribute("username") != null) {
            User user = (User) session.getAttribute("username");
            if (user.getLogStat() == 0) {
                if (request.getParameter("idBerita") == null) {
                    daftarBerita = db.getBerita();
                    request.setAttribute("daftarBerita", daftarBerita);
                    return "beritaDaftar.jsp";
                } else {
                    Long idBerita = Long.parseLong(request.getParameter("idBerita"));
                    db.deleteBerita(idBerita);
                    daftarBerita = db.getBerita();
                    request.setAttribute("daftarBerita", daftarBerita);
                    return "beritaDaftar.jsp";
                }
            } else {
                daftarBerita = db.getBerita();
                request.setAttribute("daftarBerita", daftarBerita);
                return "beritaPublic.jsp";
            }
        } else {
            daftarBerita = db.getBerita();
            request.setAttribute("daftarBerita", daftarBerita);
            return "beritaPublic.jsp";
        }
    }

    public String bukaEditBerita() {
        Berita berita = new Berita();
        DaftarBerita ou = new DaftarBerita();
        HttpSession session = request.getSession();

        if (session.getAttribute("username") != null) {
            Long idBerita = Long.parseLong(request.getParameter("idBerita"));
            berita = ou.getBerita(idBerita);
            session.setAttribute("berita", berita);
            return "editBerita.jsp";
        } else {
            return "sudahLogout.jsp";
        }
    }

    public String submitEditBerita() {
        Berita berita = new Berita();
        DaftarBerita ou = new DaftarBerita();
        HttpSession session = request.getSession();

        String judulBerita = request.getParameter("judulBerita");
        String ringkasan = request.getParameter("ringkasan");
        String kategori = request.getParameter("kategori");
        String deskripsi = request.getParameter("deskripsi");
        Long idBerita = Long.parseLong(request.getParameter("idBerita"));

        if (session.getAttribute("username") != null) {
            berita = ou.getBerita(idBerita);
            berita.setJudulBerita(judulBerita);
            berita.setRingkasan(ringkasan);
            berita.setDeskripsi(deskripsi);
            berita.setKategori(kategori);
            session.setAttribute("berita", berita);

            ou.editBerita(berita);
            return "bacaBerita.jsp";
        } else {
            return "sudahLogout.jsp";
        }
    }

    public String bacaBerita() {
        Berita berita = new Berita();
        DaftarBerita ou = new DaftarBerita();

        Long idBerita = Long.parseLong(request.getParameter("idBerita"));
        berita = ou.getBerita(idBerita);
        request.setAttribute("berita", berita);
        return "bacaBerita.jsp";
    }
}
